package com.example.websocketdemo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.websocketdemo.API.VideoOneApi;
import com.example.websocketdemo.API.VideoTwoApi;
import com.example.websocketdemo.config.WebSocketTest;
import com.example.websocketdemo.entity.video.Cameras;
import com.example.websocketdemo.entity.video.PreviewURLs;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/videohhh")
public class VideoController {
    @GetMapping("/one")
    public void monitoring(HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Cameras cameras = new Cameras();
        cameras.setPageNo(1);
        cameras.setPageSize(500);
        VideoOneApi videoOneApi = new VideoOneApi();
        String videoList = videoOneApi.C(cameras);
        JSONObject jsonObject = JSONObject.parseObject(videoList);
        System.out.println("所有数据一共有：：：：：：：：" + videoList);
        System.out.println("***********************************************************************************");
        JSONArray list = (JSONArray) ((JSONObject) jsonObject.get("data")).get("list");
        System.out.println("================所有信息==============");
        for (int i = 0; i < list.size(); i++) {
            JSONObject object = (JSONObject) list.get(i);
            PreviewURLs previewURLs = new PreviewURLs();
            previewURLs.setCameraIndexCode((String) object.get("cameraIndexCode"));
            previewURLs.setStreamType(0);
            previewURLs.setProtocol("hls");
            previewURLs.setTransmode(1);
            previewURLs.setExpand("transcode=0");
            previewURLs.setStreamform("ps");
            String url = videoOneApi.P(previewURLs);
            JSONObject dizhi = JSONObject.parseObject(url);
            System.out.println("监控点流：：：：：："+"监控点名称：：：："+object.get("cameraName")+"：：：监控点url：：："+((JSONObject)dizhi.get("data")).get("url"));
        }
    }

    @GetMapping("/two")
    public void emergencySupplies(HttpServletResponse response) throws IOException {
        Cameras cameras = new Cameras();
        cameras.setPageNo(1);
        cameras.setPageSize(500);
        VideoTwoApi videoTwoApi = new VideoTwoApi();
        String videoList = videoTwoApi.C(cameras);
        JSONObject jsonObject = JSONObject.parseObject(videoList);
        System.out.println("所有数据一共有：：：：：：：：" + videoList);
        System.out.println("***********************************************************************************");
        JSONArray list = (JSONArray) ((JSONObject) jsonObject.get("data")).get("list");
        System.out.println("================所有信息==============");
        for (int i = 0; i < list.size(); i++) {
            JSONObject object = (JSONObject) list.get(i);
            PreviewURLs previewURLs = new PreviewURLs();
            previewURLs.setCameraIndexCode((String) object.get("cameraIndexCode"));
            previewURLs.setStreamType(0);
            previewURLs.setProtocol("hls");
            previewURLs.setTransmode(1);
            previewURLs.setExpand("transcode=0");
            previewURLs.setStreamform("ps");
            String url = videoTwoApi.P(previewURLs);
            JSONObject dizhi = JSONObject.parseObject(url);
            System.out.println("监控点流：：：：：："+"监控点名称：：：："+object.get("cameraName")+"：：：监控点url：：："+((JSONObject)dizhi.get("data")).get("url"));
        }
    }
}
